package com.fxwater.admin.controller.biz;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fxwater.biz.domain.FxwDataInfo;
import com.fxwater.biz.domain.FxwElementDataInfo;
import com.fxwater.biz.domain.PumpStationInfo;
import com.fxwater.biz.domain.StationElementInfo;
import com.fxwater.biz.domain.UnitInfo;
import com.fxwater.biz.service.IStationElementService;
import com.fxwater.common.annotation.Log;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;
import com.fxwater.common.enums.BusinessType;
import com.fxwater.common.utils.poi.ExcelUtil;
import com.fxwater.framework.util.ShiroUtils;

/**
 * 泵站组件管理
 * @author tx
 *
 */
@RequestMapping("/biz/stationElement")
@Controller
public class StationElementController extends BaseController {
	
	 private static final Logger log = LoggerFactory.getLogger(StationElementController.class);
	 
	 private String prefix = "biz/stationElement";
	 
	 @Autowired
	 private IStationElementService stationElementService ;
	 
	 
	 /**
		 * 修改息状态
		 * @return
		 */
		@RequestMapping("/changeStatus")
		@ResponseBody
		 @RequiresPermissions("biz:se:edit")
		public AjaxResult changeStatus(String id,String status) {
			StationElementInfo stationElementInfo = new StationElementInfo();
			stationElementInfo.setId(Long.parseLong(id));
			stationElementInfo.setIsValid(status);
			return toAjax(stationElementService.editStationElementInfo(stationElementInfo));
		}
	 
	 
	 /**
	  * 跳转组件页面
	  * @return
	  */
	 @GetMapping("")
	 @RequiresPermissions("biz:se:view")
	 public String stationElement() {
		 return prefix+"/stationElement";
	 }
	 
	 @GetMapping("/data")
	 @RequiresPermissions("biz:stationElement:dataView")
	 public String data() {
		 return prefix+"/data";
	 }
	 
	 
	 /**
	  * 泵站组件数据查询
	  * @param stationElementInfo
	  * @return
	  */
	 @RequiresPermissions("biz:stationElement:dataList")
	 @PostMapping("/data/dataList")
	 @ResponseBody
	 public TableDataInfo dataList (FxwElementDataInfo fxwElementDataInfo) {
		 startPage();
		 List<FxwElementDataInfo> list = stationElementService.selectElementDataInfoList(fxwElementDataInfo);
		 return getDataTable(list);
	 } 
	 
	 
	 @Log(title = "组件数据管理", businessType = BusinessType.EXPORT)
	 @RequiresPermissions("biz:stationElement:dataExport")
	 @PostMapping("/data/dataExport")
	 @ResponseBody
	 public AjaxResult export(FxwElementDataInfo fxwElementDataInfo)
	 {
	     List<FxwElementDataInfo> list = stationElementService.selectElementDataInfoList(fxwElementDataInfo);
	     ExcelUtil<FxwElementDataInfo> util = new ExcelUtil<FxwElementDataInfo>(FxwElementDataInfo.class);
	     return util.exportExcel(list, "泵站组件数据");
	 }
	 
	 
	 @RequiresPermissions("biz:se:list")
	 @PostMapping("/list")
	 @ResponseBody
	 public TableDataInfo list (StationElementInfo stationElementInfo) {
		 startPage();
		 List<StationElementInfo> list = stationElementService.selectStationElementInfoList(stationElementInfo);
		 return getDataTable(list);
	 } 
	 
	 @RequiresPermissions("biz:se:list")
	 @PostMapping("/detailList")
	 @ResponseBody
	 public TableDataInfo detailList (String stationId) {
		 startPage();
		 StationElementInfo stationElementInfo = new StationElementInfo();
		 stationElementInfo.setStationId(Long.parseLong(stationId));
		 List<StationElementInfo> list = stationElementService.selectStationElementInfoList(stationElementInfo);
		 return getDataTable(list);
	 } 
	 
	 
	 @RequestMapping("/checkSerialNumberUnique")
	 @ResponseBody
	 public String checkSerialNumberUnique(String type,String serialNumber) {
		 StationElementInfo stationElementInfo  = new StationElementInfo();
		 stationElementInfo.setType(type);
		 stationElementInfo.setSerialNumber(serialNumber);
		 return stationElementService.checkSerialNumberUnique(stationElementInfo);
	 }
	 
	 /**
	  * 删除
	  * @return
	  */
	 @RequestMapping("/remove")
	 @ResponseBody
	 @RequiresPermissions("biz:se:remove")
	 @Log(title = "泵站组件删除", businessType = BusinessType.DELETE)
	 public AjaxResult remove(String ids) {
		 return toAjax(stationElementService.removestationElementInfo(ids));
	 }
	 
	 
	 /**
	  * 增加泵站组件
	  * @param mmap
	  * @return
	  */
	 @GetMapping("/add")
	 public String add(ModelMap mmap,String stationId,String stationName) {
		 if(StringUtils.isEmpty(stationId)) {
			 return prefix + "/add";
		 }else {
			 mmap.put("stationId", stationId);
			 mmap.put("stationName", stationName);
			 return prefix + "/detailAdd";
		 }
		 
	 }
	 
	 /**
	  * 添加泵站组件
	  * @param user
	  * @return
	  */
	 @RequiresPermissions("biz:se:add")
	 @Log(title = "泵站组件", businessType = BusinessType.INSERT)
	 @PostMapping("/add")
	 @ResponseBody
	 public AjaxResult addSave(StationElementInfo stationElementInfo)
	 {
		 stationElementInfo.setCreateBy(ShiroUtils.getLoginName());//
	     return toAjax(stationElementService.addStationElementInfo(stationElementInfo));
	 }
	 
	 /**
	  * 添加泵站组件
	  * @param user
	  * @return
	  */
	 @RequiresPermissions("biz:se:add")
	 @Log(title = "泵站组件", businessType = BusinessType.INSERT)
	 @RequestMapping("/detailAdd")
	 @ResponseBody
	 public AjaxResult detailAddSave(StationElementInfo stationElementInfo)
	 {
		 stationElementInfo.setCreateBy(ShiroUtils.getLoginName());//
	     return toAjax(stationElementService.addStationElementInfo(stationElementInfo));
	 }
	 
	 /**
	  * 跳转修改页面
	  * @param mmap
	  * @return
	  */
	 @GetMapping("edit/{id}")
	 public String edit ( ModelMap mmap,@PathVariable("id") String id) {
		 StationElementInfo stationElementInfo = new StationElementInfo();
		 stationElementInfo.setId(Long.parseLong(id));
		 stationElementInfo =  stationElementService.selectStationElementInfoList(stationElementInfo).get(0);
		 mmap.addAttribute("stationElementInfo", stationElementInfo);
		 return prefix + "/edit";
	 }
	 
	 /**
	  * 保存泵站修改信息
	  * @return
	  */
	 @RequiresPermissions("biz:se:edit")
	 @Log(title = "泵站管理", businessType = BusinessType.UPDATE)
	 @PostMapping("edit")
	 @ResponseBody
	 public AjaxResult editSave(StationElementInfo stationElementInfo) {
		 
		 return toAjax(stationElementService.editStationElementInfo(stationElementInfo));
	 }
	 

}
